package com.lzy.cloud.order.controller;

import com.lzy.cloud.order.dtos.OrderInfoDetailDto;
import com.lzy.cloud.order.request.OrderInfoDetailRequest;
import com.lzy.cloud.order.response.OrderInfoDetailResponse;
import com.lzy.cloud.order.service.OrderInfoDetailService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:订单总表附属表
 * @author: liyun
 * @create: 2018-07-05 13:49
 */
@RestController
@RequestMapping("/order/detail")
public class OrderInfoDetailController {
    @Autowired
    private OrderInfoDetailService orderInfoDetailService;

    /**
     * 根据订单id查询订单总表附属表
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/find/by/order/id/{orderId}",method = RequestMethod.GET)
    public Result queryByOrderId(@PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00010003");
        OrderInfoDetailDto orderInfoDetailDto = orderInfoDetailService.find(orderId);
        return new Result(DozerUtils.map(orderInfoDetailDto, OrderInfoDetailResponse.class));
    }

    /**
     * 新增订单总表附属表
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create(@RequestBody OrderInfoDetailRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Boolean ok = orderInfoDetailService.create(DozerUtils.map(request, OrderInfoDetailDto.class));
        Ensure.that(ok).isTrue("00080001");
        return new Result();
    }

    /**
     * 更新订单总表附属表
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateOrderInfoDetail(@RequestBody OrderInfoDetailRequest request) {
        Ensure.that(request).isNotNull("00000002");
        boolean result = orderInfoDetailService.update(DozerUtils.map(request, OrderInfoDetailDto.class));
        Ensure.that(result).isTrue("00080003");
        return new Result(result);
    }
}
